//
//  Favorite_View.m
//  SecondPage
//
//  Created by winston on 13-4-10.
//  Copyright (c) 2013年 winston. All rights reserved.
//

#import "Favorite_View.h"
#import "../../ResourceLoader/ResourceLoader/ResourceLoader.h"
#import "FavoriteModuleView.h"
#import "MyCarFavoriteView.h"
#import "MyCarSQLHelp.h"

#define SpaceDistance     24
#define ContentWidth      656 
#define ContentHeight     586 
#define AnimationDuration 0.4

@implementation Favorite_View
@synthesize autoID,moduleKey,rootpath,favoriteConfig,configViews;

- (id)initWithFrame:(CGRect)frame autoID:(NSString*)_autoID ModuleID:(NSString *)_moduleID
{
    self = [super initWithFrame:frame];
    if (self)
    {
        self.autoID = _autoID;
        self.moduleKey = @"Favorite";
        showindex = 0;
        

        self.rootpath = [[ResourceLoader getInstance] getModulePathByKey:moduleKey withAutoID:autoID];
        self.configViews = [NSMutableArray array];
        [[MyCarSQLHelp getInstance] SetDataBasePath:[NSString stringWithFormat:@"%@/mycar.sqlite",rootpath]];
        
        UIImageView * background = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, frame.size.width, frame.size.height)];
        [self addSubview:background];
        [background release];
        [background setImage:[UIImage imageNamed:@"AllAuto_Image_BG"]];
        
        int viewwidth = ContentWidth+2*SpaceDistance;
        contentView = [[UIScrollView alloc] initWithFrame:CGRectMake((self.frame.size.width-viewwidth)/2, 42,viewwidth, ContentHeight)];
        [self addSubview:contentView];
        [contentView release];
        contentView.scrollEnabled = NO;
        
        timelineView = [[TimeLineView alloc] initWithFrame:CGRectMake(contentView.frame.origin.x, contentView.frame.origin.y+contentView.frame.size.height + 15, contentView.frame.size.width, 80)];
        [self addSubview:timelineView];
        [timelineView release];
        
//        previousBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, contentView.frame.origin.y, 50, contentView.frame.size.height)];
//        [self addSubview:previousBtn];
//        previousBtn.contentEdgeInsets = UIEdgeInsetsMake((previousBtn.frame.size.height-50)/2, 0, (previousBtn.frame.size.height-50)/2, (previousBtn.frame.size.width-23));
//        [previousBtn release];
//        [previousBtn setImage:[UIImage imageNamed:@"AllAuto_Button_Previous_BG1"] forState:UIControlStateNormal];
//        [previousBtn addTarget:self action:@selector(previousView) forControlEvents:UIControlEventTouchUpInside];
//        
//        nextBtn = [[UIButton alloc] initWithFrame:CGRectMake(self.frame.size.width-50, contentView.frame.origin.y, 50, contentView.frame.size.height)];
//        [self addSubview:nextBtn];
//        nextBtn.contentEdgeInsets = UIEdgeInsetsMake((nextBtn.frame.size.height-50)/2, (nextBtn.frame.size.width-23), (nextBtn.frame.size.height-50)/2, 0);
//        [nextBtn release];
//        [nextBtn setImage:[UIImage imageNamed:@"AllAuto_Button_Next_BG1"] forState:UIControlStateNormal];
//        [nextBtn addTarget:self action:@selector(nextView) forControlEvents:UIControlEventTouchUpInside];
        
        recognizer_Right = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeRight:)];
        [self addGestureRecognizer:recognizer_Right];
        [recognizer_Right release];
        
        recognizer_Left = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeLeft:)];
        [recognizer_Left setDirection:UISwipeGestureRecognizerDirectionLeft];
        [self addGestureRecognizer:recognizer_Left];
        [recognizer_Left release];
        
        [self initData];
    }
    return self;
}

-(void)LoadFavoriteConfig
{
    NSString * resourcePath = [[NSBundle mainBundle] resourcePath];
    NSString * configPath = [resourcePath stringByAppendingPathComponent:@"/FavoriteConfig.plist"];
    
    self.favoriteConfig = [NSArray arrayWithContentsOfFile:configPath];
    [timelineView RefreshByFavorite:favoriteConfig];
    
}

-(FavoriteModuleView *)LoadFileView:(NSString *)title AndIndex:(int)index
{
    if (title == nil || [title isEqualToString:@""]) {
        return nil;
    }
    
    Class targetClass = NSClassFromString([NSString stringWithFormat:@"%@FavoriteView",title]);
    FavoriteModuleView * tempView = [[targetClass alloc]initWithFrame:CGRectMake(contentView.frame.size.width*showindex+SpaceDistance, 0,ContentWidth, ContentHeight)];
    tempView.parentselectIndex = index;
    tempView.resourcePath = [NSString stringWithFormat:@"%@/%@",rootpath,title];
    tempView.categoryName = title;
    tempView.autoID = self.autoID;
    [tempView ReadConfig];
    
    if ([tempView isKindOfClass:[MyCarFavoriteView class]]) {
        NSString * panorama =[[ResourceLoader getInstance] getHomePageAutoImagePath:autoID];
        [tempView performSelector:@selector(RefreshPanoramaImage:) withObject:panorama];
        [tempView performSelector:@selector(RefreshByOtherData:) withObject:configViews];
    }
    
    return tempView;
}

-(void)initData
{
    [self LoadFavoriteConfig];
    
    //显示第一个视图
    showindex = -1;
    [self nextView];
}

-(void)previousView
{
    --showindex;
    if (showindex<0) {
        ++showindex;
        return;
    }
//    if (showindex - 1 < 0) {
//        previousBtn.hidden = YES;
//    }
//    else
//    {
//        previousBtn.hidden = NO;
//    }
//    nextBtn.hidden = NO;

    [timelineView SetShowIndex:showindex];
    
    
    [UIView animateWithDuration:AnimationDuration
                     animations:^{
                         [contentView setContentOffset:CGPointMake(contentView.frame.size.width * showindex, 0)];
                         
                     }
                     completion:^(BOOL finished){
                         FavoriteModuleView * lastview = [configViews lastObject];
                         [configViews removeObject:lastview];
                         [lastview removeFromSuperview];
                         contentView.contentSize = CGSizeMake(contentView.frame.size.width*(showindex +1), contentView.frame.size.height);
                     }];
}

-(void)nextView
{
    ++showindex;
    if (favoriteConfig.count >showindex) {
        
//        if (showindex + 1 == favoriteConfig.count) {
//            nextBtn.hidden = YES;
//        }
//        else
//        {
//            nextBtn.hidden = NO;
//        }
//        if (showindex == 0) {
//            previousBtn.hidden = YES;
//        }
//        else
//        {
//            previousBtn.hidden = NO;
//        }
        
        //上一个视图选择的项，决定了下一个视图显示什么
        FavoriteModuleView * preview = [configViews lastObject];
        int preselectindex = -1;
        if (preview) {
            preselectindex = preview.selectIndex;
        }
        
        
        NSDictionary * dic = [favoriteConfig objectAtIndex:showindex];
        FavoriteModuleView * tempview = [self LoadFileView:[dic objectForKey:@"DocumentPath"] AndIndex:preselectindex];
        if(tempview)
        {
            [configViews addObject:tempview];
            [contentView addSubview:tempview];
            [tempview release];
        }
        
        
        [timelineView SetShowIndex:showindex];
        
        contentView.contentSize = CGSizeMake(contentView.frame.size.width*(showindex +1), contentView.frame.size.height);
        [UIView animateWithDuration:AnimationDuration
                         animations:^{
                             [contentView setContentOffset:CGPointMake(contentView.frame.size.width * showindex, 0)];
                             
                         }
                         completion:^(BOOL finished){

                         }];
    }
    else
    {
        --showindex;
    }
    
}

-(void)handleSwipeLeft:(UISwipeGestureRecognizer *)recognizer {
    
    [self nextView];
}

-(void)handleSwipeRight:(UISwipeGestureRecognizer *)recognizer {
    [self previousView];
}

-(void)dealloc
{
    self.autoID = nil;
    self.moduleKey = nil;
    self.rootpath = nil;
    self.configViews = nil;
    self.favoriteConfig = nil;
    [[MyCarSQLHelp getInstance] closeDatabase];
    [super dealloc];
}

@end
